<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#">
  <!ENTITY dc "http://purl.org/dc/elements/1.1/">
  <!ENTITY dcterms "http://purl.org/dc/terms/">
  <!ENTITY foaf "http://xmlns.com/foaf/0.1/">
  <!ENTITY a "http://xmlns.heustics.org/schema/myelin/0.1/">
  <!ENTITY m "http://xmlns.heustics.org/ontology/myelin/0.1/">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
  xmlns:date="http://exslt.org/dates-and-times"
  xmlns:exslt="http://exslt.org/common"
  xmlns:xf="http://www.w3.org/2002/xforms" 
  xmlns:l="http://xmlns.heustics.org/schema/i18n/0.1/"
  xmlns:a="&a;"
  xmlns:o="&a;dates#"
  exclude-result-prefixes="xsl a l xf date exslt"
>
  <xsl:variable name="o:l10n" select="a:get-l10n('&a;dates')"/>    
    
  <xsl:template match="*" mode="o:input-hidden" name="o:input-hidden">
    <xsl:param name="name" select="local-name()"/>
    <xsl:param name="date" select="string(.)"/>
    <xsl:choose>
      <xsl:when test="string-length($date)">
        <xsl:apply-templates select="date:tokenize(date:adjust-date-to-timezone($a:timezone,$date))" mode="o:parts-input-hidden">
          <xsl:with-param name="name" select="$name"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="ud">
          <y/><M/><d/><H/><m/><s/><z/>
        </xsl:variable>
        <xsl:apply-templates select="exslt:node-set($ud)/*" mode="o:parts-input-hidden">
          <xsl:with-param name="name" select="$name"/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template> 
  
  <xsl:template match="*" mode="o:parts-input-hidden">
    <xsl:param name="name"/>
    <input type="hidden" name="{concat($name,'-',local-name())}" value="{.}"/>
  </xsl:template>
    
  <xsl:template match="*" name="o:input" mode="o:input">    
    <xsl:param name="name" select="local-name()"/>
    <xsl:param name="config" select="/*/a:config"/>
    <xsl:param name="date" select="string(.)"/>
    <xsl:choose>
      <xsl:when test="string-length($date)">
        <xsl:apply-templates select="date:tokenize(date:adjust-date-to-timezone($a:timezone,$date))" mode="o:parts-input">
          <xsl:with-param name="config" select="$config"/>
          <xsl:with-param name="name" select="$name"/>
          <xsl:with-param name="l10n" select="$o:l10n/l:context[@name='date']/*"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="ud">
          <y/><M/><d/><H/><m/><s/><z/>
        </xsl:variable>
        <xsl:apply-templates select="exslt:node-set($ud)/*" mode="o:parts-input">
          <xsl:with-param name="config" select="$config"/>
          <xsl:with-param name="name" select="$name"/>
          <xsl:with-param name="l10n" select="$o:l10n/l:context[@name='date']/*"/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose> 
  </xsl:template>
  
  <xsl:template match="y" mode="o:parts-input">
    <xsl:param name="name"/>
    <xsl:param name="l10n"/>
    <div class="entry date year">
      <div class="header">
        <span>
          <xsl:value-of select="$l10n[@key='y']"/>
        </span>
        <div class="spacer"/>
      </div>
      <div class="options">
        <input type="text" name="{concat($name,'-y')}" maxlength="4" size="3">
          <xsl:variable name="y" select="string(.)"/>
          <xsl:attribute name="value">
            <xsl:if test="string-length($y)">
              <xsl:value-of select="$y"/>
            </xsl:if>
          </xsl:attribute>        
        </input>
        <div class="spacer"/>
      </div>
      <div class="spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="M" mode="o:parts-input">
    <xsl:param name="name"/>
    <xsl:param name="l10n"/>
    <div class="entry date month">
      <div class="header">
        <span>
          <xsl:value-of select="$l10n[@key='M']"/>
        </span>
        <div class="spacer"/>
      </div>
      <div class="options">
        <xsl:variable name="selected" select="string(.)"/>
        <select name="{concat($name,'-M')}">
          <option value=""/>
          <xsl:for-each select="$o:l10n/l:context[@name='date-month-abbrev']/*">
            <option value="{@key}">
              <xsl:if test="@key = $selected">
                <xsl:attribute name="selected">selected</xsl:attribute>
              </xsl:if>
              <xsl:value-of select="."/>
            </option>
          </xsl:for-each>         
        </select>
        <div class="spacer"/>
      </div>     
      <div class="spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="d" mode="o:parts-input">
    <xsl:param name="name"/>
    <xsl:param name="l10n"/>
    <div class="entry date day">
      <div class="header">
        <span>
          <xsl:value-of select="$l10n[@key='d']"/>
        </span>
        <div class="spacer"/>
      </div>
      <div class="options">
        <select name="{concat($name,'-d')}">
          <option value=""/>
          <xsl:call-template name="a:select-number">
            <xsl:with-param name="cur" select="1"/>
            <xsl:with-param name="max" select="31"/>
            <xsl:with-param name="step" select="1"/>
            <xsl:with-param name="selected" select="number(.)"/>
          </xsl:call-template>
        </select>
        <div class="spacer"/>
      </div>     
      <div class="spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="H" mode="o:parts-input">
    <xsl:param name="name"/>
    <xsl:param name="l10n"/>
    <div class="entry date hour">
      <div class="header">
        <span>
          <xsl:value-of select="$l10n[@key='H']"/>
        </span>
        <div class="spacer"/>
      </div>
      <div class="options">
        <select name="{concat($name,'-H')}">
          <option value=""/>
          <xsl:call-template name="a:select-number">
            <xsl:with-param name="cur" select="0"/>
            <xsl:with-param name="max" select="23"/>
            <xsl:with-param name="step" select="1"/>
            <xsl:with-param name="selected" select="number(.)"/>
          </xsl:call-template>
        </select>
        <div class="spacer"/>
      </div>     
      <div class="spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="m" mode="o:parts-input">
    <xsl:param name="name"/>
    <xsl:param name="l10n"/>
    <div class="entry date minute">
      <div class="header">
        <span>
          <xsl:value-of select="$l10n[@key='m']"/>
        </span>
        <div class="spacer"/>
      </div>
      <div class="options">
        <select name="{concat($name,'-m')}">
          <option value=""/>
          <xsl:call-template name="a:select-number">
            <xsl:with-param name="cur" select="0"/>
            <xsl:with-param name="max" select="59"/>
            <xsl:with-param name="step" select="1"/>
            <xsl:with-param name="selected" select="number(.)"/>
          </xsl:call-template>
        </select>
        <div class="spacer"/>
      </div>     
      <div class="spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="s" mode="o:parts-input">
    <xsl:param name="name"/>
    <xsl:param name="l10n"/>
    <div class="entry date second">
      <div class="header">
        <span>
          <xsl:value-of select="$l10n[@key='s']"/>
        </span>
        <div class="spacer"/>
      </div>
      <div class="options">
        <select name="{concat($name,'-s')}">
          <option value=""/>
          <xsl:call-template name="a:select-number">
            <xsl:with-param name="cur" select="0"/>
            <xsl:with-param name="max" select="59"/>
            <xsl:with-param name="step" select="1"/>
            <xsl:with-param name="selected" select="round(number(.))"/>
          </xsl:call-template>
        </select>
        <div class="spacer"/>
      </div>     
      <div class="spacer"/>
    </div>
  </xsl:template>
    
  <xsl:template match="z" mode="o:parts-input">
    <xsl:param name="name"/>
    <xsl:param name="l10n"/>
    <div class="entry date timezone">
       <div class="header">
        <span>
          <xsl:value-of select="$l10n[@key='z']"/>
        </span>
        <div class="spacer"/>
      </div>
      <div class="options">
        <span>
          <xsl:value-of select="$a:timezone"/>
        </span>
        <input type="hidden" name="{concat($name,'-z')}" value="{$a:timezone}"/>
        <div class="spacer"/>
      </div>     
      <div class="spacer"/>
    </div>
  </xsl:template>
  
</xsl:stylesheet>
